// pages/shopCar/order/order.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    totalDisCount:0,
    payCurInfo: {},
    address: {},
    time: "",
    getAddress: {},
    totalPrice: 0,
    payGoods: [],
    orderList: [],
    isInv: false, //发票
    payWay: "在线支付", //支付方式
    curPayIndex: 0, //支付方式下标
    isHint: false, //支付方式提醒
    isPay: false, //支付方式
    hint: "修改支付方式，会影响已选的配送方式，提单前，请确认好信息",

    isDelivery: false, //配送
    expressage: "若社区村镇无人员出入管控，京东快递可送货上门",
    kalendar: [],

    ticket_intro:
      "京东自用启用电子普通发票，与纸质普通发票具备同等法律效力，订单完成后24小时内在“我的订单”查看",
    billIndex: 0, //发票抬头下标
    billCIndex: 0, //发票内容下标
    billName: "", //发票名称
    bill: "电子(商品明细-个人)", //发票
    distributionIndex: 0, //配送点击
    distributionBottomIndex: 0,
    distributionBottomRightIndex: 0,
    bill_rise: [
      {
        id: 1,
        header: "个人",
        info: [
          {
            id: 2,
            name: "个人名称",
            place: "请填写 '个人' 或者您的姓名",
          },
        ],
      },
      {
        id: 3,
        header: "单位",
        info: [
          {
            id: 4,
            name: "单位名称",
            place: "请填写单位名称",
          },
          {
            id: 5,
            name: "纳税人识别号",
            place: "请在此填写纳税人识别号",
          },
          {
            id: 6,
            name: "注册地址",
            place: "请填写注册地址",
          },
          {
            id: 7,
            name: "注册电话",
            place: "请填写注册电话",
          },
          {
            id: 8,
            name: "开户银行",
            place: "请填写单位开户银行",
          },
          {
            id: 9,
            name: "银行账号",
            place: "请填写单位银行账号",
          },
        ],
      },
    ],
    bill_contain: [
      {
        id: 1,
        classify: "商品明细",
        intro: [
          {
            id: 2,
            contain:
              "发票内容将显示详细商品名称与价格信息，发票金额为实际支付金额，不含虚拟资产、优惠等扣减金额。",
          },
        ],
      },
      {
        id: 3,
        classify: "商品类型",
        intro: [
          {
            id: 4,
            contain:
              "发票内容将显示本单商品所属类别(饮品饮料)及价格信息，发票金额为实际支付金额，不含虚拟资产、优惠等扣减金额。",
          },
        ],
      },
    ],
    dispatching: [
      {
        id: 1,
        type: "京东快递",
        time: ["09:00-15:00", "15:00-21:00"],
      },
      {
        id: 2,
        type: "上门自提",
        time: [{}],
      },
    ],
    payment: [
      {
        id: 1,
        way: "在线支付",
      },
      {
        id: 2,
        way: "货到付款",
      },
    ],

    // 滑块
    swi_statue: false, //滑块状态
  },
  // 配送
  delivery_time() {
    this.setData({
      isDelivery: true,
    });
    this.selectComponent("#distribution").show();
  },
  // 支付方式
  pay_func() {
    this.setData({
      isPay: true,
      isHint: false,
    });
  },
  // 关闭提醒
  clone_hint() {
    this.setData({
      isHint: true,
    });
  },
  // 关闭
  clone() {
    this.setData({
      isPay: false,
      isDelivery: false,
      isInv: false,
    });
  },
  // 获取支付方式
  getSet_way(e) {
    this.setData({
      curPayIndex: e.currentTarget.dataset.index,
      payWay: e.currentTarget.dataset.item,
    });
  },
  // 返回支付方式
  GetpayWay() {
    this.clone();
    return this.data.payWay;
  },

  // 日历
  calendar() {
    let today = new Date();
    let oneday = 24 * 3600 * 1000;
    let watt = today.getDay();
    let base = new Date(today).getTime();

    let arr = [];
    for (let temp = 0; temp < 7; temp++) {
      let tempTime = new Date((base += oneday));
      switch (tempTime.getDay()) {
        case 0:
          watt = "周日";
          break;
        case 1:
          watt = "周一";
          break;
        case 2:
          watt = "周二";
          break;
        case 3:
          watt = "周三";
          break;
        case 4:
          watt = "周四";
          break;
        case 5:
          watt = "周五";
          break;
        case 6:
          watt = "周六";
          break;
      }
      arr.push(tempTime.getMonth() + 1 + "月" + tempTime.getDate() + "日" + "[" + watt + "]");
    }
    this.setData({
      kalendar: arr
    })
    return this.data.kalendar;
  },
  // 获取送货时间
  getimmedistely() {
    this.setData({});
  },

  // 发票蒙层
  pay_bill() {
    this.setData({
      isInv: true,
    });
  },
  // 发票抬头
  getHead(e) {
    this.setData({
      billIndex: e.currentTarget.dataset.index,
    });
  },
  // 发票内容
  getContain(e) {
    this.setData({
      billCIndex: e.currentTarget.dataset.index,
    });
  },
  // 返回发票
  GetBill() {
    this.clone();
    this.setData({
      bill:
        "电子" +
        "(" +
        this.data.bill_contain[this.data.billCIndex].classify +
        "-" +
        this.data.billName +
        ")",
    });
    return this.data.bill;
  },

  // 滑块状态
  switch_statue(e) {
    this.setData({
      swi_statue: e.detail.value,
    });
  },

  // 跳转至地址页
  toAddress() {
    console.log(this.data.getAddress.address_id)
    wx.navigateTo({
      url: "/pages/shopCar/address/address?id=" + this.data.getAddress.address_id,
    });
  },
  //配货点击事件
  destributionItemClick(e) {
    let index = e.currentTarget.dataset.index;

    this.setData({
      distributionIndex: index,
    });
  },
  destributionItemBottomClick(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      distributionBottomIndex: index,
    });
  },
  destributionItemBottomRightClick(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      distributionBottomRightIndex: index,
    });
  },
  // 生成订单
 async payByOneself() {
    let list = this.data.payGoods;
    let that = this;
    list = list.map((item) => {
      return item.shopping_car_id
    })
   await wx.request({
      url: 'http://api_devs.wanxikeji.cn/api/generateOrder',
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: {
        token: "3c35c3d0738a879f23926f4676b2874e",
        address_id: this.data.getAddress.address_id,
        money: this.data.totalPrice,
        shopping_car_ids: list,
        coupon_money:this.data.totalPrice - this.data.totalDisCount
      },
      success: function (res) {
        console.log(res);
        that.setData({ address: res.data.data })
        console.log(that.data.address.paySign);
        console.log(res.data.data.paySign)
        wx.requestPayment({
          timeStamp: that.data.address.timeStamp,
          nonceStr: that.data.address.nonce_str,
          package: 'prepay_id=' + that.data.address.prepay_id,
          signType: 'MD5',
          paySign: that.data.address.paySign,
          success(res) {
            console.log(res);
            wx.navigateTo({
  
              url: '../../../pages/mine/orderList/orderList',
            })
          },
          fail(res) {
            console.log(res);
          }
        })
      }
    });
  },
  // 配送返回
  dis() {
    this.setData({

      time: this.data.kalendar[this.data.distributionBottomIndex] + this.data.dispatching[0].time[this.data.distributionBottomRightIndex],
      isDelivery: false
    })
    console.log(this.data.time);

    return this.data.time
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let list = JSON.parse(options.list);
    console.log(list)
    let total = 0;
    let totalDisCount = 0;
    for (let i = 0; i < list.length; i++) {
      total += list[i].num * list[i].price
      totalDisCount += Number(list[i].money)
    }
    that.setData({
      payGoods: list,
      totalPrice: total.toFixed(2),
      totalDisCount:totalDisCount.toFixed(2)
    });
    wx.request({
      url: 'http://api_devs.wanxikeji.cn/api/userAddressList?token=3c35c3d0738a879f23926f4676b2874e',
      method: "POST",
      success: function (res) {
        let adds = res.data.data;
        adds = adds.map((item) => {
          item.selected = false;
          return item;
        })
        that.setData({
          getAddress: res.data,
        });
        console.log(res.data);
      },
    });

    wx.request({
      url: 'http://api_devs.wanxikeji.cn/api/userAddressList?token=3c35c3d0738a879f23926f4676b2874e',
      method: "POST",
      success: function (res) {
        let adds = res.data.data;
        adds = adds.map((item) => {
          item.selected = false;
          return item;
        })
        that.setData({
          getAddress: adds[0],
        })
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.calendar();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.payByOneself();
    let that = this;
    let pages = getCurrentPages();
    let curPage = pages[pages.length - 1];
    curPage.setData({
      getAddress: that.data.getAddress
    })
    console.log(that.data.getAddress)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () { },
});
